package com.leo;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: duan
 * @Date: Create in 15:19 2020/10/14
 */
public class MyServer {

    @Test
    public void server() {
        try {
            ServerSocket ss = new ServerSocket(8080);
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            byte[] b = new byte[1024];
            int len;
            while((len = is.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            s.getOutputStream().write("完成".getBytes());
            System.out.println("服务器完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void client() {
        StringBuilder sb = new StringBuilder("GET " + "/demo/index.jsp" + " HTTP/1.1\r\n");
        sb.append("Host: localhost:8080\r\n");
        sb.append("User-Agent: Mozilla/5.0\r\n");
        sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
        sb.append("Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\n");
        sb.append("Accept-Encoding: gzip, deflate\r\n");
        sb.append("Connection: keep-alive\r\n");
        sb.append("Cookie: JSESSIONID=690A7E4BF312F3B9E04342489B694AAF; Hm_lvt_8b02a318fde5831da10426656a43d03c=1595235007; _ga=GA1.1.1505077159.1595235008\r\n");
        sb.append("Upgrade-Insecure-Requests: 1\r\n");
        sb.append("Cache-Control: max-age=0\r\n");

        try {
            Socket s = new Socket("localhost", 8080);
            s.getOutputStream().write(sb.toString().getBytes());
            System.out.println(sb.toString());
//            Thread.sleep(2000);

            InputStream is = s.getInputStream();
            int size = is.available();
            System.out.println("size:::" + size);
            byte[] b = new byte[1024];
            int len = is.read(b);
            System.out.println(new String(b, 0, len));
            System.out.println("客户端完成");
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test//不能是static方法
    public void t2Server() {
        try {
            System.out.println("启动服务端");
            ServerSocket server = new ServerSocket(9527);
            Socket accept = server.accept();//等socket客户端连接
//			System.out.println(accept);
            InputStream is = accept.getInputStream();
            int size = is.available();
            System.out.println("size:::" + size);
            byte[] b = new byte[1024];
            int len = -1;
            while((len = is.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            accept.shutdownInput();
            System.out.println(new String(b));
            OutputStream os = accept.getOutputStream();
            os.write("服务端发回的信息".getBytes());
            System.out.println("服务端关闭");
            accept.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t2Client() {
        StringBuilder sb = new StringBuilder("GET " + "/demo/index.jsp" + " HTTP/1.1\r\n");
        sb.append("Host: localhost:8080\r\n");
        sb.append("User-Agent: Mozilla/5.0\r\n");
        sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
        sb.append("Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\n");
        sb.append("Accept-Encoding: gzip, deflate\r\n");
        sb.append("Connection: keep-alive\r\n");
        sb.append("Cookie: JSESSIONID=690A7E4BF312F3B9E04342489B694AAF; Hm_lvt_8b02a318fde5831da10426656a43d03c=1595235007; _ga=GA1.1.1505077159.1595235008\r\n");
        sb.append("Upgrade-Insecure-Requests: 1\r\n");
        sb.append("Cache-Control: max-age=0\r\n");

        try {
            Socket s = new Socket(InetAddress.getLocalHost(), 8080);
            OutputStream os = s.getOutputStream();
            os.write(sb.toString().getBytes());
            s.shutdownOutput();
            Thread.sleep(2000);

            InputStream is = s.getInputStream();
            int size = is.available();
            System.out.println("size:::" + size);
            byte[] b = new byte[1024];
            int len = -1;
            while((len = is.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            s.shutdownInput();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
